package site.shihuan.helpdesk.ticket.manager;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.shihuan.helpdesk.common.util.IdWorker;
import site.shihuan.helpdesk.security.context.UserContext;
import site.shihuan.helpdesk.ticket.dao.TktFileDao;
import site.shihuan.helpdesk.ticket.dao.TktLogTicketDao;
import site.shihuan.helpdesk.ticket.dao.TktTicketDao;
import site.shihuan.helpdesk.ticket.model.po.TktLogTicketPO;
import site.shihuan.helpdesk.ticket.model.po.TktTicketPO;
import site.shihuan.helpdesk.ticket.model.ticket.TicketLogType;
import site.shihuan.helpdesk.ticket.model.vo.TicketVO;

import java.util.List;

/**
 * 工单业务
 *
 * @author 周世焕
 * @Date 2021/4/4 14:45
 */
@Service
public class TicketManager {

    @Autowired
    private TktTicketDao ticketDao;

    @Autowired
    private TktLogTicketDao logTicketDao;

    @Autowired
    private TktFileDao fileDao;

    /**
     * 保存工单
     * @param ticketPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public TktTicketPO saveTicket(TktTicketPO ticketPO) {
        if(ticketPO.getTicketId() != null) {
            ticketDao.updateByPrimaryKeySelective(ticketPO);
            TktLogTicketPO logTicketPO = new TktLogTicketPO();
            logTicketPO.setLogId(IdWorker.nextId());
            logTicketPO.setLogType(TicketLogType.UPDATE);
            logTicketPO.setTicketId(ticketPO.getTicketId());
            logTicketPO.setLogData(JSONUtil.toJsonStr(ticketPO));
            logTicketPO.setUserId(UserContext.getUserId());
            logTicketDao.insertSelective(logTicketPO);
            return ticketPO;
        }
        ticketPO.setTicketId(IdWorker.nextId());
        ticketDao.insertSelective(ticketPO);
        TktLogTicketPO logTicketPO = new TktLogTicketPO();
        logTicketPO.setLogId(IdWorker.nextId());
        logTicketPO.setLogType(TicketLogType.CREATE);
        logTicketPO.setTicketId(ticketPO.getTicketId());
        logTicketPO.setLogData(JSONUtil.toJsonStr(ticketPO));
        logTicketPO.setUserId(UserContext.getUserId());
        logTicketDao.insertSelective(logTicketPO);
        return ticketPO;
    }

    /**
     * 更改工单状态
     * @param tenantId
     * @param ticketId
     * @param old
     * @param now
     */
    @Transactional(rollbackFor = Exception.class)
    public void changeTicketStatus(String tenantId, String ticketId, String old, String now) {
        TktTicketPO ticketPO = new TktTicketPO();
        ticketPO.setTicketId(ticketId);
        ticketPO.setTenantId(tenantId);
        ticketPO.setStatus(now);
        ticketDao.updateByPrimaryKeySelective(ticketPO);
        TktLogTicketPO logTicketPO = new TktLogTicketPO();
        logTicketPO.setLogId(IdWorker.nextId());
        logTicketPO.setLogType(TicketLogType.CHANGE_STATUS);
        logTicketPO.setTicketId(ticketPO.getTicketId());
        logTicketPO.setLogData("{\"old\":\""+old+"\",\"now\":\""+now+"\"}");
        logTicketPO.setUserId(UserContext.getUserId());
        logTicketDao.insertSelective(logTicketPO);
    }

    /**
     * 查询工单列表
     * @param tenantId
     * @return
     */
    public List<TicketVO> findAllTicket(String tenantId) {
        return ticketDao.selectAll(tenantId);
    }

    /**
     * 工单分配
     * @param tenantId
     * @param ticketId
     * @param assignee
     * @param assigneeName
     */
    @Transactional(rollbackFor = Exception.class)
    public void assignTicket(String tenantId, String ticketId, String assignee, String assigneeName) {
        TktTicketPO ticketPO = new TktTicketPO();
        ticketPO.setTicketId(ticketId);
        ticketPO.setTenantId(tenantId);
        ticketPO.setAssignee(assignee);
        ticketPO.setAssigneeName(assigneeName);
        ticketDao.updateByPrimaryKeySelective(ticketPO);
        TktLogTicketPO logTicketPO = new TktLogTicketPO();
        logTicketPO.setLogId(IdWorker.nextId());
        logTicketPO.setLogType(TicketLogType.ASSIGN);
        logTicketPO.setTicketId(ticketId);
        logTicketPO.setLogData(assigneeName);
        logTicketPO.setUserId(UserContext.getUserId());
        logTicketDao.insertSelective(logTicketPO);
    }

    /**
     * 工单分配（批量）
     * @param tenantId
     * @param ticketIdList
     * @param assignee
     * @param assigneeName
     */
    @Transactional(rollbackFor = Exception.class)
    public void assignTicket(String tenantId, String[] ticketIdList, String assignee, String assigneeName) {
        for (String ticketId : ticketIdList) {
            assignTicket(tenantId, ticketId, assignee, assigneeName);
        }
    }

    /**
     * 查看组内工单
     * @param tenantId
     * @param deptId
     */
    public List<TicketVO> findDeptTicket(String tenantId, String deptId) {
        return ticketDao.selectByDeptAssigned(tenantId, deptId);
    }

    /**
     * 查看我的工单
     * @param tenantId
     * @param userId
     * @param status
     */
    public List<TicketVO> findUserTicket(String tenantId, String userId, String status) {
        return ticketDao.selectByUserAssigned(tenantId, userId, status);
    }

    /**
     * 查看客户工单
     * @param tenantId
     * @param customerId
     * @param status
     */
    public List<TicketVO> findCustomerTicket(String tenantId, String customerId, String status) {
        return ticketDao.selectByCustomer(tenantId, customerId, status);
    }

    /**
     * 查询工单ById
     * @param ticketId
     * @return
     */
    public TicketVO findById(String ticketId) {
        return ticketDao.selectById(ticketId);
    }

    /**
     * 删除工单ById
     * @param ticketId
     * @return
     */
    public void deleteById(String ticketId) {
        ticketDao.deleteByPrimaryKey(ticketId);
    }

    /**
     * 删除工单ById（批量）
     * @param ticketIdList
     * @return
     */
    public void deleteById(String[] ticketIdList) {
        for(String ticketId : ticketIdList) {
            ticketDao.deleteByPrimaryKey(ticketId);
        }
    }
}
